package org.example.operation;
import java.util.*;
import org.example.model.*;
import org.example.trans.*;
import org.example.inout.*;

/**
 * @author lenovo
 */

public class ScoreOperation {
    //加载HashMap
    public static void loadScore(){
        if(Save.getHashMapScore() == null){
            Save.setHashMapScore(TransScore.transScoreReadScore(InOut.read
                    ("saveScore.txt")));
        }
        if(Save.getHashMapScore() == null){
            Save.setHashMapScore(new HashMap<>());
        }
    }
    //将courseName和score添加到num对应值中，再添加到HashMap中，并写入文件
    public static void addScore(String num,String courseName, double score){
        CourseOperation.loadCourse();
        loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        Course course = new Course();
        CourseScore courseScore = new CourseScore();
        course.setCourseName(courseName);
        courseScore.setCourse(course);
        courseScore.setScore(score);
        hashMap.get(num).getScore().put(courseScore.getCourse().getCourseName(),courseScore);
        Save.setHashMapScore(hashMap);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
    //删除HashMap中num对应值的courseName，并写入文件
    public static void deleteScore(String num,String courseName){
        CourseOperation.loadCourse();
        loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        hashMap.get(num).getScore().remove(courseName);
        Save.setHashMapScore(hashMap);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
    //检测HashMap中num对应值是否存在courseName
    public static boolean isScore(String num,String courseName){
        loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        return hashMap.get(num).getScore().containsKey(courseName);
    }
    //模糊查找HashMap中num包含num的Map.Entry，返回String
    public static String searchNumScore(String num){
        loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        StringBuilder stringBuilder = new StringBuilder();
        if(hashMap.containsKey(num)){
            stringBuilder.append("学号：").append(num).append("\n");
            stringBuilder.append("姓名：").append(hashMap.get(num).getStudent().
                    getName()).append("\n");
            stringBuilder.append("性别：").append(hashMap.get(num).getStudent().
                    getGender()).append("\n");
            stringBuilder.append("出生日期：").append(hashMap.get(num).getStudent().
                    getBirthday()).append("\n");
            for (Map.Entry<String, CourseScore> courseScore :
                    hashMap.get(num).getScore().entrySet()) {
                stringBuilder.append(courseScore.getKey()).append("：").append(String.format
                        ("%.2f", courseScore.getValue().getScore())).append("\n");
            }
            stringBuilder.append("\n");
        }
        for(Map.Entry<String,Score> map : hashMap.entrySet()){
            if(!map.getKey().equals(num) && map.getKey().contains(num)){
                stringBuilder.append("学号：").append(map.getKey()).append("\n");
                stringBuilder.append("姓名：").append(map.getValue().getStudent().
                        getName()).append("\n");
                stringBuilder.append("性别：").append(map.getValue().getStudent().
                        getGender()).append("\n");
                stringBuilder.append("出生日期：").append(map.getValue().getStudent().
                        getBirthday()).append("\n");
                for (Map.Entry<String, CourseScore> courseScore :
                        map.getValue().getScore().entrySet()) {
                    stringBuilder.append(courseScore.getKey()).append("：").append(String.format
                            ("%.2f", courseScore.getValue().getScore())).append("\n");
                }
                stringBuilder.append("\n");
            }
        }
        return new String(stringBuilder);
    }
    //模糊查找HashMap中name包含name的Map.Entry，返回String
    public static String searchNameScore(String name){
        loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        for(Map.Entry<String,Score> map : hashMap.entrySet()){
            if(map.getValue().getStudent().getName().equals(name)){
                stringBuilder1.append("学号：").append(map.getKey()).append("\n");
                stringBuilder1.append("姓名：").append(map.getValue().getStudent().
                        getName()).append("\n");
                stringBuilder1.append("性别：").append(map.getValue().getStudent().
                        getGender()).append("\n");
                stringBuilder1.append("出生日期：").append(map.getValue().getStudent().
                        getBirthday()).append("\n");
                for (Map.Entry<String, CourseScore> courseScore :
                        map.getValue().getScore().entrySet()) {
                    stringBuilder1.append(courseScore.getKey()).append("：").append(String.format
                            ("%.2f", courseScore.getValue().getScore())).append("\n");
                }
                stringBuilder1.append("\n");
            }else if(map.getValue().getStudent().getName().contains(name)){
                stringBuilder2.append("学号：").append(map.getKey()).append("\n");
                stringBuilder2.append("姓名：").append(map.getValue().getStudent().
                        getName()).append("\n");
                stringBuilder2.append("性别：").append(map.getValue().getStudent().
                        getGender()).append("\n");
                stringBuilder2.append("出生日期：").append(map.getValue().getStudent().
                        getBirthday()).append("\n");
                for (Map.Entry<String, CourseScore> courseScore :
                        map.getValue().getScore().entrySet()) {
                    stringBuilder2.append(courseScore.getKey()).append("：").append(String.format
                            ("%.2f", courseScore.getValue().getScore())).append("\n");
                }
                stringBuilder2.append("\n");
            }
        }
        stringBuilder1.append(stringBuilder2);
        return new String(stringBuilder1);
    }
    //清空HashMap中每一个Map.Entry的值的score
    public static void clearScore(){
        CourseOperation.loadCourse();
        loadScore();
        HashMap<String,Score> hashMapScore = Save.getHashMapScore();
        for(Map.Entry<String,Score> map : hashMapScore.entrySet()){
            map.getValue().getScore().clear();
            hashMapScore.put(map.getKey(),map.getValue());
        }
        Save.setHashMapScore(hashMapScore);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
}
